﻿using System;

namespace Common.Utils.Helper
{
    public static class TimeHelper
    {
        public static DateTime dtOriginTime = new DateTime(1970, 1, 1);


        public enum TimeType
        {
            Millisecond, //毫秒
            Second,      //秒
            Minute,      //分钟
            Hour,        //小时
            Day          //天
        }

        /// <summary>  
        /// 函数执行耗时
        /// </summary>
        /// <param name="_func">执行的函数</param>  
        /// <returns>double类型：耗费时间</returns>  
        public static double FuncCostTime(Action _func)
        {
            DateTimeOffset sdt = DateTimeOffset.UtcNow;
            _func();
            DateTimeOffset edt = DateTimeOffset.UtcNow;
            return (edt - sdt).TotalMilliseconds;
        }


        /// <summary>  
        /// 获取时间戳Timestamp
        /// </summary>  
        /// <returns></returns>  
        public static double GetCurrentTimeStamp(TimeType timeType = TimeType.Second)
        {
            //根据当地的时区获取对应当地的格林威治时间
            DateTime DateStart = TimeZone.CurrentTimeZone.ToLocalTime(dtOriginTime);

            //计算时间差
            TimeSpan timeSpan = DateTime.Now - DateStart;

            switch (timeType)
            {
                case TimeType.Millisecond:
                    return timeSpan.TotalMilliseconds;
                case TimeType.Second:
                    return Math.Round(timeSpan.TotalSeconds, 0);    //取时间 秒 且不要小数
                case TimeType.Minute:
                    return Math.Round(timeSpan.TotalMinutes, 0);
                case TimeType.Hour:
                    return Math.Round(timeSpan.TotalHours, 0);
                case TimeType.Day:
                    return Math.Round(timeSpan.TotalDays, 0);
            }
            return 0;
        }


        /// 时间戳转为C#格式时间  
        /// </summary>  
        /// <param name=”timeStamp”></param>  
        /// <returns></returns>  
        public static DateTime GetTimeWithTimeSpan(double timeDif, TimeType timeType = TimeType.Second)
        {
            //根据当地的时区获取对应当地的格林威治时间
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(dtOriginTime);

            switch (timeType)
            {
                case TimeType.Millisecond:
                    return dtStart.AddMilliseconds((int)timeDif);
                case TimeType.Second:
                    return dtStart.AddSeconds(timeDif);
                case TimeType.Minute:
                    return dtStart.AddMinutes(timeDif);
                case TimeType.Hour:
                    return dtStart.AddHours(timeDif);
                case TimeType.Day:
                    return dtStart.AddDays(timeDif);
            }
            return dtStart;
        }

        /// <summary>
        /// 获取当前时间戳精确到毫秒
        /// </summary>
        /// <returns></returns>
        public static double GetCurrentTimeStampMillisecond()
        {
            return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000;
        }

        /// <summary>
        /// 获取当前时间戳精确到秒
        /// </summary>
        /// <returns></returns>
        public static double GetCurrentTimeStampSecond()
        {
            return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
        }

        /// <summary>
        /// 获取当前时间戳精确到分钟
        /// </summary>
        /// <returns></returns>
        public static double GetCurrentTimeStampMinute()
        {
            return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 600000000;
        }

        /// <summary>
        /// 获取当前时间戳精确到小时
        /// </summary>
        /// <returns></returns>
        public static double GetCurrentTimeStampHour()
        {
            return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 36000000000;
        }

        /// <summary>
        /// 获取当前时间戳精确到天
        /// </summary>
        /// <returns></returns>
        public static double GetCurrentTimeStampDay()
        {
            return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 864000000000;
        }

        /// <summary>
        /// 获取当前日期
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentDateWithYMD()
        {
            DateTime dt = DateTimeOffset.UtcNow.LocalDateTime;
            return dt.ToString("yyyyMMdd");
        }
    }
}
